package com.lagou.io;

public class ClassLoaderWrapper {
    ClassLoader[] getClassLoaders(ClassLoader classLoader) {
        return new ClassLoader[]{
                classLoader,
                Thread.currentThread().getContextClassLoader(),
                getClass().getClassLoader(),
                ClassLoader.getSystemClassLoader()};
    }

    Class<?> classForName(String name, ClassLoader[] classLoader) throws ClassNotFoundException {
        // 遍历 ClassLoader 数组
        for (ClassLoader cl : classLoader) {
            if (null != cl) {
                try {
                    // 获得类
                    Class<?> c = Class.forName(name, true, cl);

                    // 成功获得到，返回
                    if (null != c) {
                        return c;
                    }
                } catch (ClassNotFoundException e) {
                    // we'll ignore this until all classloaders fail to locate the class
                }
            }
        }
        // 获得不到，抛出 ClassNotFoundException 异常
        throw new ClassNotFoundException("Cannot find class: " + name);
    }
}
